package com.jay.android;

import com.jay.android.fragment.NavigationFragment;
import com.jay.android.obd.OBDDevices;
import com.jay.android.view.PercentView;

import android.support.v4.app.FragmentManager;
import android.support.v7.app.ActionBarActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

@SuppressLint({ "NewApi", "HandlerLeak", "ShowToast" })
public class MainActivity extends ActionBarActivity {

	private Toast mToast;
	private NavigationFragment navigationFragment;
	private PercentView percentView;
	private ImageButton scanBtn;
	private TextView statusTextView;
	private int percent;
	private int targetPercent = 0;
	protected final Handler mHandler = new Handler();

	protected Runnable percentUpdater = new Runnable() {
		@Override
		public void run() {
			percent = percentView.getPercent();
			if (percent < targetPercent) {
				percent += 1;
				percentView.setPercent(percent);
				mHandler.postDelayed(percentUpdater, 10);
				percentView.scaning();
			} else if (percent > targetPercent) {
				percent -= 1;
				percentView.setPercent(percent);
				mHandler.postDelayed(percentUpdater, 10);
				percentView.scaning();
			} else {
				percentView.scanDone();
				scanBtn.setEnabled(true);
			}
		}
	};

	private final Handler mOBDHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case OBDDevices.OBD_CONNECT_STATE:
				int state = msg.arg1;
				if (state == OBDDevices.OBD_CONNECT_STATE_CONNECTED) {
					statusTextView.setText("已经成功连接上OBD盒子");
				} else if (state == OBDDevices.OBD_CONNECT_STATE_DISCONNECTED) {
					statusTextView.setText("OBD盒子失去连接");
				} else if (state == OBDDevices.OBD_CONNECT_STATE_CONNECTING) {
					statusTextView.setText("正在连接中...");
				} else if (state == OBDDevices.OBD_CONNECT_STATE_TIMEOUT) {
					statusTextView.setText("连接超时，请尝试重新连接");
				}
				break;
			default:
				break;
			}
		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		// getWindow().setFlags(
		// WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
		// WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
		setContentView(R.layout.activity_main);
		resourceInit();
		runToPercent(50);
		mToast = Toast.makeText(this, "", Toast.LENGTH_SHORT);
		OBDDevices.getInstance().init(this);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		OBDDevices.getInstance().register(mOBDHandler);
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		OBDDevices.getInstance().unregister();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		OBDDevices.getInstance().release();
	}

	private void runToPercent(int percent) {
		targetPercent = percent;
		mHandler.postDelayed(percentUpdater, 500);
		scanBtn.setEnabled(false);
	}

	void resourceInit() {
		percentView = (PercentView) findViewById(R.id.percentView);
		scanBtn = (ImageButton) findViewById(R.id.scan_btn);
		statusTextView = (TextView) findViewById(R.id.car_status_text);
		scanBtn.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View view) {
				// TODO Auto-generated method stub
				OBDDevices.getInstance().connect();
			}
		});

		navigationFragment = new NavigationFragment();

		FragmentManager fm = getSupportFragmentManager();
		fm.beginTransaction().replace(R.id.main_bottom, navigationFragment)
				.commit();
	}

	private void showTip(final String str) {
		runOnUiThread(new Runnable() {
			@Override
			public void run() {
				mToast.setText(str);
				mToast.show();
			}
		});
	}
}
